(function(app){
	app
		.controller("content_20_inputCtrl",["$scope","$state","myFactory","$http","$stateParams","$q","$ionicHistory",function($scope,$state,myFactory,$http,$stateParams,$q,$ionicHistory){
			//参数定义、接收
			var itemId = $stateParams.changeTypeId;//变更项目ID
			var chdrnum01 =  $stateParams.chdrnum01; //保单号
			var chdrcoy = $stateParams.chdrcoy; //保单公司号码
			var policyData;//保单详情
			var itemData;//用于接收初始化查询initContentItem的内容，里面存在各种各样的内容，比如item02_1
			var initFlag = false;//检测标志，在初始化函数完成后，会被置为true
			//绑定参数到页面
			$scope.chdrnum01 = chdrnum01;
			$scope.readFlag = false;//阅读协议标志，默认为false
			var token=sessionStorage.getItem("token");
			//重新受理
			$scope.renew = function(){
				myFactory.renew();
			}	
			//退出移动保全
			$scope.exit = function(){
				myFactory.exit();
			}
			//返回上一页
			$scope.goBack = function(){
				myFactory.goBack();
			}
			//页面同意阅读协议复选框部分
			$scope.changeReadFlag = function(){
				$scope.readFlag = !$scope.readFlag;
			}

			//分配比例动态下拉
			$scope.changeRate = function(index){
				var options = $("#BuyAccountRateSelect_"+index+" option:selected");
				$("#BuyAccountRateInput_"+index).val(options.val());
				
				//合计
				var sum = 0
				for(var i=0;i<$scope.inputItem20.length;i++){
					var val = $("#BuyAccountRateInput_"+i).val();
					if($("#BuyAccountRateInput_"+i).val() == null || $("#BuyAccountRateInput_"+i).val() == ''){
						val = 0;
					}
					sum += parseInt(val);
				}
				$("#account_selection_sum").text(sum);
			}

/**********************************************初始化函数begin****************************************************/
			//初始化查询变更项内容
			$scope.load = function () {
				myFactory.loadingShow();//显示加载圈
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-token-timestamp";
				var signStr="itemId="+itemId+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化查询变更项内容
				$http({
					method: 'GET',
					url: app.domain + 'input/initContentItem.action',
					params: {'itemId': itemId,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						itemData = response.data.data.modelMap;
						$scope.inputItem20 = itemData.inputItem20;
						console.log($scope.inputItem20);
						for(var i=0;i<$scope.inputItem20.length;i++){
							$scope.inputItem20[i].funddesc=$scope.inputItem20[i].funddesc.replace("投资账户","");
						}
						//遍历一下列表，如果对于每一项的alfnds字段有数值，则默认为该数值
						for(var i=0;i<$scope.inputItem20.length;i++){
							if($scope.inputItem20[0].alfnds!=null 
									&& $scope.inputItem20[0].alfnds!="" 
									&& $scope.inputItem20[0].alfnds%5==0 ){//如果该值不是空的，而且能被5整除，那么对应投资项比例设置为该值
								$("#BuyAccountRateSelect_"+i).val($scope.inputItem20[0].alfnds);
								//$scope["BuyAccountRateSelect_"+i] = $scope.inputItem20[0].alfnds; 暂时无法使用这个方式，空白的问题没能解决
							}
						}
						initFlag=true;//标识初始化成功
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","初始化查询变更项内容失败!","info");
				});
			}
/**********************************************初始化函数end****************************************************/			

			//保单详情弹框
			$scope.showPolicyDetail = function(){
				modal('open','policyModalOn20Input','overwindowOn20Input','policyCloseXOn20Input');
				var timestamp=myFactory.getTimetamp();
				var sfis= "chdrnum-chdrcoy-token-timestamp";
				var signStr="chdrnum="+chdrnum01+"&chdrcoy="+chdrcoy+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化时候查询保单详情
				$http({
					method: 'GET',
					url: app.domain + 'query/restFindPolicyDetail.action',
					params: {'chdrnum': chdrnum01,'chdrcoy': chdrcoy,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						policyData = response.data.data;
						$scope.policyData = policyData;
						$scope.policyData.ptdate01=myFactory.DateTools($scope.policyData.ptdate01);
					}
				}, function errorCallback(response) {
					myFactory.alert("","初始化查询订单详情失败!","info");
				});
			}
			//保单详情弹窗关闭函数
			$scope.policyCancelOn20Input = function(){
				modal('close','policyModalOn20Input','overwindowOn20Input','policyCloseXOn20Input');
			}
			//关闭遮罩
			$scope.overwindowClose = function(){
				//$(".overwindow").show();
				$(".overwindow").hide();
				$(".overmodal").hide();
			}
			

			//下一步
			$scope.nextStep = function(){
				myFactory.loadingShow();//显示加载圈
				//判断是否初始化完成了，如果失败，不予进入下一步
				if(!initFlag){
					myFactory.alert("","初始化时失败，请返回上一步重试","info");
					myFactory.loadingHide();
					return;
				}
				
		    	var xd=false;
	        	var account=0;
	        	/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 start*/
	        	//当次买入/转入投资账户不可超10个
	        	var accountRateCount = 0;
	        	/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 end*/
	        	var c1 = $("select[name='BuyAccountRateSelect']");//得到所有select框
				for(var i=0;i<c1.length;i++){
					var v=$("#BuyAccountRateSelect_"+i+" option:selected").val();
					if(isNaN(v)){//检测得到的选项是不是数字，原程序中现在只有当初始不选的时候，可能导致不是数字，这是强制等于0
            			v = "0";
            		}
					v = v +".00";//得到具体某项的新的分配比例值
					var s=$("#sacscurbal_"+i).text().trim();//得到具体某项原来的分配比例值
					account=account+v*1;	//把新的分类值累计到accout中
				
					if(v==s&&xd==false){ //针对每一项投资变更，如果所有新的分配比例跟原来相同，那么不给下一步
						xd=false;  //如果始终没走过else流程，说明新的分配跟原来的一毛一样，不给下一步
					}else{
						xd=true;
					}
					
					 /*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 start*/
				    if("" != v.trim()){
				    	accountRateCount++;
				    }
				    /*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 end*/
				}
				
				/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 start*/
				if(accountRateCount>10){
					myFactory.alert("","当次买入/转入投资账户不可超10个","info");
					myFactory.loadingHide();
					return false;
				}
			    /*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 end*/
				
				if(account==0){
					myFactory.alert("","新投资账户分配比例为0，需重新确认!","info");
					myFactory.loadingHide();
					return false;
				}
				if(account!=100){
					myFactory.alert("","新投资账户分配比例和不等于100%，需重新确认!","info");
					myFactory.loadingHide();
					return false;
				}		
		        if(xd==false){
		        	myFactory.alert("","变更内容未发生变化,请重新确认!","info");
		        	myFactory.loadingHide();
					return false;
		        }
		        myFactory.loadingHide();
		        //所有校验通过，包装数据准备上传
		        var a = $("input[name='BuyAccountType']");//买入类型
		        var b = $("input[name='BuyAccountRateInput']");//买入百分比
		        var c = $("input[name='BuyAccountName']");//账户中文
		        var buyType = "";
		        var buyRate = "";
		        var buyName = "";
		        
		        for(var i=0;i<a.length;i++){//买入类型封装
		        	if(i!=0){
		        		buyType = buyType + ",";
		        	}
		        	buyType = buyType + $("#BuyAccountType_"+i).val().trim();
		        }
		        
		        for(var i=0;i<b.length;i++){
		        	if(i!=0){
		        		buyRate = buyRate + ",";
		        	}
		        	var value = $("#BuyAccountRateInput_"+i).val().trim();
		        	if( value ==""){ //如果值为空，则默认是0
		        		value = "0";
		        	}
		        	buyRate = buyRate + value;
		        }
		        
		        for(var i=0;i<c.length;i++){
		        	if(i!=0){
		        		buyName = buyName + ",";
		        	}
		        	buyName = buyName + $("#BuyAccountName_"+i).val().trim();
		        }
		        
		        var item={
		        	"buyType":buyType,
		        	"buyRate":buyRate,
		        	"buyName":buyName,
		        	"itemId":itemId
		        }
		        
    			/*
    			 * ********************************************************************
    			 * 	《S2018082805259-投资风险测评保全系统需求》（add by jiangwy 20190531 ) star
    			 * 
    			 * 1、检查风险评测参数信息
    			 * 2、是否进入评测页面判断
    			 * 
    			 *********************************************************************/  
		        /*
		        $ionicHistory.clearCache().then(function(){ 
					$state.go('content_check_main',{
						itemId:itemId,
						item:item,
						chdrnum01:chdrnum01,
						chdrcoy:chdrcoy
					});
				})
				*/
		        var timestamp=myFactory.getTimetamp();
				var sfis= "token-timestamp";
				var signStr="token=" + token + "&timestamp=" + timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				var reqData = {
						"chdrcoy":chdrcoy,
						"chdrnum":chdrnum01,
						"itemId": itemId,
						"itemStr":item
				}
    			$http({
    				method: 'POST',
    				url: app.domain + 'riskevaluate/checkRisk.action',
    				data:{paramsMap:reqData,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
    			}).then(function successCallback(response) {
    				if (response.data.success) {
    					var resultData = response.data.data.modelMap;  
    					//	【本次买入账户风险等级】
    					$scope.inputRiskGrade = resultData.inputRiskGrade;
    					//	是否需要风险评测
    					$scope.isEvaluate     = resultData.isEvaluate;
    					//	路由 nextUrl 地址，默认为：审核页面
    					var nextUrl = "content_check_main";
    					//	如果风险评测结果值为“N”，则直接跳过该风险评测页面进入审核页面
    					if( "Y" == $scope.isEvaluate ){
    						//	进入风险评测页面
    						nextUrl = "invest_risk_evaluate";
    					}
    					
    					//	路由跳转
//		                $ionicHistory.clearCache().then(function(){
		                    $state.go( nextUrl ,{
		                        itemId:itemId,
		                        item:item,
		                        chdrnum01:chdrnum01,
		                        chdrcoy:chdrcoy,
		                        inputRiskGrade:$scope.inputRiskGrade
		                    });
//		                })
    				}
    				myFactory.loadingHide();
    			}, function errorCallback(response) {
    				myFactory.loadingHide();
    				myFactory.alert("","初始化查询风险评测失败!","info");
    			});
    			
   			 	/* 
    			 * ********************************************************************
    			 * 	《S2018082805259-投资风险测评保全系统需求》（add by jiangwy 20190531 ) end
    			 * 
    			 * ********************************************************************/ 
    			
		        
//				$state.go("account_selection_result");
			}
					
		}]);   
		//指针弹窗
		app.directive('infoid',function(){
			return {
				restrict: 'EA',		//指明指令在DOM里面以什么形式被声明,EA.表示即可以是元素也可以是属性
				templateUrl: 'html/input/content_base_info.html'+app.clearCache,
				replace: true,		//设置解析后，DOM声明的指令是否消失
				link: function(scope,element,attrs){  //链接函数负责注册DOM事件和更新DOM,
					console.log('指令完成');
				}
			}
		});
})(app)